import 'package:flutter/material.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/rendering.dart';
import 'package:shopping_group/entity/rich_text_entity.dart';
import 'package:shopping_group/event/event.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'log_util.dart';
import 'object_util.dart';
import 'route_util.dart';
import 'widget_utils.dart';
import 'package:video_player/video_player.dart';

class VideoPlayerUtil {
  VideoPlayerController _controller;
  BuildContext mContext;
  bool isOpen = false;
  bool _initialized = false;
  String _time = '0';
  void initVideoPlayer(String url, {Function videoblackListener}) {
    if (ObjectUtil.isEmptyString(url)) {
      return;
    }
    _controller = VideoPlayerController.network(url)
      ..initialize().then((_) {
        BotToast.closeAllLoading();
        _initialized = true;
        _time = _controller.value.duration.inSeconds.toString();
        eventBus.fire(VideoTimeEvent(_time));
        _controller.play();
        videoblackListener();
      });
    _controller.addListener(_videoValueListener);
    BotToast.showLoading();
  }

  Widget initView(BuildContext context,
      {double height: 200, double horizontalPadding: 16, String time}) {
    mContext = context;
    if (null == _controller) {
      return Container();
    }
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.symmetric(horizontal: 0),
      // height: height, // 这里随意
      child: Stack(
        children: [
          _initialized
              ? AspectRatio(
                  aspectRatio: _controller.value.aspectRatio,
                  child: VideoPlayer(_controller),
                )
              : Container(),
          initCostomView(time: time),
        ],
      ),
    );
  }

  Widget initCostomView(
      {double height: 200,
      double horizontalPadding: 16,
      double width: 460,
      Function blackCallback,
      String time}) {
    return Container(
      color: Colours.transparent,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            // height: 52,
            // width: width,
            color: Colours.transparent,
            child: Row(
              children: <Widget>[
                SizedBox(
                  width: 32,
                ),
                //_closeView(),
                Expanded(child: Container()),
                SizedBox(
                  width: 8,
                ),
                _showTimeView(time),
                SizedBox(
                  width: 32,
                ),
              ],
            ),
          ),
          // Expanded(child: Container()),
          // HandleWidget(),
          // SizedBox(
          //   height: 8,
          // ),
        ],
      ),
    );
  }

  Widget _closeView() {
    return GestureDetector(
      onTap: () {
        _showAdView();
      },
      child: Container(
        width: 32,
        height: 32,
        margin: const EdgeInsets.symmetric(vertical: 32, horizontal: 16),
        decoration: BoxDecoration(
            border: Border.all(color: Colours.transparent_80, width: 1.0),
            borderRadius: BorderRadius.circular(60.0),
            color: Colours.transparent_80),
        child: Icon(
          Icons.close,
          color: Colours.title_white,
          size: 16,
        ),
      ),
    );
  }

  Widget _showTimeView(time) {
    List<RichTextEntity> list = [];
    list
      ..add(RichTextEntity()
        ..backgroundColor = Colours.transparent
        ..title = '剩余 '
        ..textColor = Colours.title_white
        ..size = Dimens.FONT_SIZE_14)
      ..add(RichTextEntity()
        ..backgroundColor = Colours.transparent
        ..title = time
        ..textColor = Colours.red_606
        ..size = Dimens.FONT_SIZE_14)
      ..add(RichTextEntity()
        ..backgroundColor = Colours.transparent
        ..title = ' 秒'
        ..textColor = Colours.title_white
        ..size = Dimens.FONT_SIZE_14);
    return Container(
        alignment: Alignment.center,
        margin: const EdgeInsets.symmetric(vertical: 32, horizontal: 16),
        child: WidgetUtils.getRichTextView(list));

    // Container(
    //   alignment: Alignment.center,
    //   margin: const EdgeInsets.symmetric(vertical: 32, horizontal: 16),
    //   child: Row(
    //     mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    //     crossAxisAlignment: CrossAxisAlignment.center,
    //     children: <Widget>[
    //       WidgetUtils.getTextColorView(
    //         '剩余 ',
    //         Colours.title_white,
    //         Dimens.FONT_SIZE_14,
    //       ),
    //       WidgetUtils.getTextColorView(
    //         time,
    //         Colours.red_606,
    //         Dimens.FONT_SIZE_14,
    //       ),
    //       WidgetUtils.getTextColorView(
    //         ' 秒',
    //         Colours.title_white,
    //         Dimens.FONT_SIZE_14,
    //       ),
    //     ],
    //   ),
    // );
  }

  Future _showAdView() async {
    showDialog(
        context: mContext,
        barrierDismissible: true,
        builder: (context) {
          return showAlertDialog();
        }).then((var result) {
      if (null == result) {
        return;
      }

      if (result is String && 'yes' == result) {
        RouteUtil.closeView();
      }
    });
  }

  Widget showAlertDialog() {
    isOpen = true;
    return SimpleDialog(
      title: WidgetUtils.getTextColorView(
          '确认关闭', Colours.themeColor, Dimens.FONT_SIZE_18),
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 4),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Padding(
                padding:
                    const EdgeInsets.symmetric(vertical: 24, horizontal: 8),
                child: WidgetUtils.getTextColorView(
                    '关闭后你将不会获取任何奖励', Colours.gray_91, Dimens.FONT_SIZE_16),
              ),
              SizedBox(
                height: 16,
              ),
              showCancelBottom()
            ],
          ),
        ),
      ],
    );
  }

  Widget showCancelBottom() {
    return Flex(
      direction: Axis.horizontal,
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        GestureDetector(
          onTap: () {
            _closeDailog('yes');
          },
          child: WidgetUtils.showBoxBotton('确认关闭',
              borderColor: Colours.gray_91,
              textColor: Colours.gray_91,
              borderRadius: 20),
        ),
        SizedBox(
          width: 8,
        ),
        GestureDetector(
          onTap: () {
            _closeDailog('');
          },
          child: WidgetUtils.showBoxBotton('继续观看',
              borderColor: Colours.themeColor,
              boxColor: Colours.themeColor,
              textColor: Colours.title_white,
              borderRadius: 20,
              horizontalPadding: 16),
        ),
      ],
    );
  }

  _closeDailog(val) {
    isOpen = false;
    Navigator.of(mContext).pop(val);
  }

  // Widget _showBlackView() {
  //   return GestureDetector(
  //     onTap: () {
  //       RouteUtil.closeView();
  //     },
  //     child: Container(
  //       width: 32,
  //       height: 32,
  //       margin: const EdgeInsets.symmetric(vertical: 32, horizontal: 16),
  //       decoration: BoxDecoration(
  //           border: Border.all(color: Colours.transparent_80, width: 1.0),
  //           borderRadius: BorderRadius.circular(60.0),
  //           color: Colours.transparent_80),
  //       child: Icon(
  //         Icons.arrow_back_ios,
  //         color: Colours.title_white,
  //         size: 16,
  //       ),
  //     ),
  //   );
  // }

  void _videoValueListener() async {
    // LogUtil.e(_time, tag: 'FijkValue completed........................');
    // if ('1' == _time) {
    //   eventBus.fire(VideoCompleteEvent());
    // }
  }

  void closeView() {
    if (isOpen) {
      _closeDailog('');
    }
  }

  void dispose() {
    if (null == _controller) {
      return;
    }
    // _controller.pause();
    if (null != _videoValueListener) {
      _controller.removeListener(_videoValueListener);
    }
    _controller.dispose();
    _controller = null;
  }
}
